package com.zmn.oms.admin.controller.common;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.admin.controller.order.OrderBaseController;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.scm2.common.model.dro.scm.factory.goods.FactoryGoodsDRO;
import com.zmn.scm2.common.model.query.scm.factory.goods.FactoryGoodsQuery;
import com.zmn.scm2.dubbo.interfaces.scm.factory.goods.FactoryGoodsListRemoteService;
import com.zmn.scm2.dubbo.interfaces.utils.DubboConsts;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类描述：产品相关
 *
 * @author liuying
 * @date 2018/11/21 15:36
 */
@Controller
@RequestMapping("/serv/factory")
public class ServFactoryController extends OrderBaseController {

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    protected FactoryGoodsListRemoteService factoryGoodsListRemoteService;

    /**
     * 获取厂商产品
     *
     * @param factoryId
     * @return
     */
    @RequestMapping(value = "product/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO productList(@RequestParam(value = "factoryId") Integer factoryId) {
        return ResultDTO.success();
    }

    /**
     * 通过厂商产品ID获取可以使用的服务类型
     *
     * @param fcProductId
     * @return
     */
    @RequestMapping(value = "product/servCateg/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO productList(int fcProductId) {
        return ResultDTO.success(super.getServCategListByFcProduct(fcProductId));
    }

    /**
     * 获取产品故障
     *
     * @param productId
     * @return
     */
    @RequestMapping(value = "product/subsidiary/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO subsidiaryList(int productId) {
        return ResultDTO.success(super.getFactorySubsidiaryList(productId));
    }

    /**
     * 厂商产品转化为基础
     *
     * @param fcProductId
     * @param servCategId
     * @return
     */
    @RequestMapping(value = "product/convertProduct", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO convertProduct(@RequestParam(value = "plat", defaultValue = "0") Integer plat, int fcProductId, int servCategId, int channelId) {
        return ResultDTO.success(super.getPrductIdByFcProduct(plat, fcProductId, servCategId, channelId));
    }

    /**
     * 查询厂商配件列表
     * @param factoryId
     * @param keyWord
     * @return
     */
    @RequestMapping(value = "getParts", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO getParts(Integer factoryId, String keyWord) {
        if (factoryId == null) {
            return ResultDTO.fail("请选择厂商");
        }
        FactoryGoodsQuery query = FactoryGoodsQuery.builder().fcId(Long.valueOf(factoryId)).goodsAlias(keyWord).build();
        ResponseDTO<List<FactoryGoodsDRO>> responseDTO = factoryGoodsListRemoteService.listFactoryGoods(query);
        if (!responseDTO.isSuccess()) {
            return ResultDTO.fail("获取厂商配件失败，请稍后重试");
        }
        List<KVDict<Integer>> parts = responseDTO.getData().stream().map(good -> {
            StringBuilder partFullName = new StringBuilder();
            partFullName.append(good.getCategOneName());
            partFullName.append("-");
            partFullName.append(good.getCategTwoName());
            partFullName.append("-");
            partFullName.append(good.getGoodsAlias());
            partFullName.append("/");
            partFullName.append(good.getUnitName());
            return KVDict.<Integer>builder()
                    .value(good.getId())
                    .text(partFullName.toString())
                    .build();
        }).collect(Collectors.toList());


        return ResultDTO.success(parts);
    }

}
